<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DeepSeek Chat</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f4f4f9;
        }
        .chat-container {
            width: 400px;
            background-color: #fff;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }
        .chat-box {
            height: 300px;
            overflow-y: auto;
            border: 1px solid #ddd;
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 4px;
            background-color: #f9f9f9;
        }
        input[type="text"] {
            width: calc(100% - 22px);
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        button {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
<div class="chat-container">
    <h1>DeepSeek Chat</h1>
    <div class="chat-box" id="chat-box"></div>
    <input type="text" id="message-input" placeholder="Type your message here...">
    <button id="send-button">Send</button>
</div>
<script>
    document.getElementById('send-button').addEventListener('click', function() {
        const message = document.getElementById('message-input').value;
        if (message.trim() === '') return;

        const chatBox = document.getElementById('chat-box');
        chatBox.innerHTML += `<div class="user-message">You: ${message}</div>`;

        const eventSource = new EventSource(`/stream?message=${encodeURIComponent(message)}`);
        let aiResponse = ""; // 用于累积完整的 AI 回答

        eventSource.onmessage = function(event) {
            if (event.data === 'end') {
                eventSource.close();
                chatBox.innerHTML += `<div class="ai-message">AI: ${aiResponse}</div>`;
                chatBox.scrollTop = chatBox.scrollHeight;
            } else {
                try {
                    const dataObj = JSON.parse(event.data);
                    aiResponse += dataObj.content || "";
                } catch (error) {
                    console.error("JSON 解析错误:", error);
                }
            }
        };

        eventSource.onerror = function() {
            chatBox.innerHTML += `<div class="error-message">Error: Unable to connect to the server.</div>`;
            eventSource.close();
        };

        document.getElementById('message-input').value = '';
    });
</script>
</body>
</html>
